/**
 * 
 */   
package com.cares.infobird.service;  

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.cares.core.base.service.BaseService;
import com.cares.core.common.service.DicService;
import com.cares.core.mng.service.ShiroDbRealm.ShiroUser;
import com.cares.entity.base.PageModel;
import com.cares.entity.common.Dic;
import com.cares.entity.infobird.CallBusi;
import com.cares.entity.infobird.CallOrder;
import com.cares.entity.infobird.OrderStat;
import com.cares.entity.infobird.OrderStatParm;
import com.cares.infobird.repository.CallBusiMybatisDao;
import com.cares.infobird.repository.CallOrderMybatisDao;
import com.cares.infobird.repository.OrderStatParmMybatisDao;

/**   
 * @作者 Stone
 * @创建日期 2014-7-26 下午12:01:43
 * @版本 V1.0
 * @类说明
 */
@Service("callOrderService")
@Transactional
public class CallOrderService extends BaseService {

	@Autowired
	private CallOrderMybatisDao callOrderDao;
	
	@Autowired
	private CallBusiMybatisDao callBusiDao;
	
	@Autowired
	private OrderStatParmMybatisDao orderStatDao;
	
	@Resource(name="callRecordService")
	private CallRecordService callRecordService;
	
	@Resource(name="dicService")
	private DicService dicService;
	
	/**
	 * 查询列表信息
	 * @param pageModel
	 * @return
	 */
	public String list(PageModel<CallOrder> pageModel) {

		String result = "";
		JSONObject json = new JSONObject();
		List<CallOrder> results = callOrderDao.getAll(pageModel);
		try {
			json.put("total", this.callOrderDao.count(pageModel));
			json.put("rows", JSON.toJSON(results));
			result = json.toString();
		} catch (JSONException e) {
		}
		return result;
	}
	
	/**
	 * 组合查询列表信息
	 * @param pageModel
	 * @return
	 */
	public String busiList(PageModel<CallBusi> pageModel) {

		String result = "";
		JSONObject json = new JSONObject();
		List<CallBusi> results = callBusiDao.getAll(pageModel);
		try {
			json.put("total", this.callBusiDao.count(pageModel));
			json.put("rows", JSON.toJSON(results));
			result = json.toString();
		} catch (JSONException e) {
		}
		return result;
	}
	
	/**
	 * 根据pageModel查询业务list
	 * @param pageModel
	 * @return
	 */
	public List<CallBusi> getBusiList(PageModel<CallBusi> pageModel) {
		return callBusiDao.getAll(pageModel);
	}
	
	/**
	 * 根据ID查询CallOrder
	 */
	public CallOrder get(Long id) {
		return callOrderDao.get(id);
	}
	
	/**
	 * 新增CallOrder
	 */
	public void save(CallOrder order) {
		order.setCreateTime(dateProvider.getDate());
		callOrderDao.save(order);
	}
	
	/**
	 * 更新CallOrder
	 */
	public void update(CallOrder order) {
		Assert.notNull(order.getId(), "缺少主键");
		order.setUpdateTime(dateProvider.getDate());
		callOrderDao.update(order);
	}
	
	/**
	 * 处理CallOrder
	 */
	public void deal(CallOrder order) {
		Assert.notNull(order.getId(), "缺少主键");
		order.setDealTime(dateProvider.getDate());
		order.setUpdateTime(dateProvider.getDate());
		callOrderDao.update(order);
	}
	
	/**
	 * 根据ID删除CallOrder
	 */
	public void delete(Long id) {
		callOrderDao.delete(id);
	}
	
	/**
	 * 根据code查找当前code下所有业务
	 * @param code
	 * @return
	 */
	public List<Dic> getParentBusiness(String code) {
		List<Dic> result = null;
		List<Dic> parms = dicService.getDicListByCode(code);
		if(parms!=null && parms.size()>0) {
			Dic parm = parms.get(0);
			result = dicService.getDicListByUpId(parm.getId());
		}
		return result;
	}
	
	/**
	 * 工单分省统计
	 * @param pageModel
	 * @return
	 */
	public List<OrderStat> getBusiProvStat(Map pramMap) {
		return callBusiDao.getBusiProvStat(pramMap);
	}
	
	/**
	 * 根据参数组合查询分省统计报表
	 * @param pageModel
	 * @return
	 */
	public String getProvStatList(PageModel<OrderStatParm> pageModel) {
		String result = "";
		JSONObject json = new JSONObject();
		List<OrderStat> results = orderStatDao.getProvStatList(pageModel);
		try {
			json.put("total", this.orderStatDao.getProvStatCnt(pageModel));
			json.put("rows", JSON.toJSON(results));
			result = json.toString();
		} catch (JSONException e) {
		}
		return result;
	}

}
